package com.link.play.fit.ai.ext

import android.view.View

/**
 * 单独的点击事件防抖处理
 * @receiver View
 * @param thresholdMillis Long  防抖动时间间隔
 * @param block Function0<Unit> 点击事件
 */
inline fun View.setThrottleListener(
    thresholdMillis: Long = 500,
    crossinline block: (v: View?) -> Unit
) {
    setOnClickListener(object : ThrottleListener {
        override val thresholdMillis: Long
            get() = thresholdMillis

        override fun onClick(v: View?) {
            super.onClick(v)
            block.invoke(v)
        }
    })
}

/**
 * 防抖动接口，一般在activity或者fragment中实现，然后统一处理每个view点击事件
 * @property thresholdMillis Long 防抖动时间间隔
 */
interface ThrottleListener : View.OnClickListener {
    val thresholdMillis: Long
        get() = 500//默认500毫秒

    override fun onClick(v: View?) {
        v?.isClickable = false
        v?.postDelayed({ v.isClickable = true }, thresholdMillis)
    }
}

inline fun View.ifNoClickListener(block: () -> Unit) {
    if (!hasOnClickListeners()) {
        block()
    }
}